/**
 * @author 袁德坤！
 * @date 2010-11-21 下午12:24:05
 */
package com.ad.services;

import java.util.List;

import com.ad.dao.IUserDao;
import com.ad.dao.IUserInfoDao;
import com.ad.hibernate.User;
import com.ad.hibernate.UserInformation;
import com.ad.system.Common;

public class UserManagerService implements IUserManagerService {

	private IUserDao userDao;

	private IUserInfoDao userInfoDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	@Override
	public String check(String userName, String password) {
		password = Common.MD5(password);
		List<User> user = this.getUserDao().Check(userName, password);
		if (user == null || user.size() == 0) {
			return "";
		}
		return user.get(0).getNickName();

		/*
		 * if(this.getUserDao().Check(userName, password)==null) { return null;
		 * } return null;
		 */
	}

	@Override
	public void insert(User user) {
		String userId = this.getUserDao().insert(user);
		UserInformation newUserInformation = new UserInformation();
		newUserInformation.setUserId(Integer.parseInt(userId));
		this.getUserInfoDao().insertUserInfor(newUserInformation);
	}

	@Override
	public String userName(String userName, String password) {
		return this.getUserDao().Check(userName, password).get(0).getNickName();
	}

	@Override
	public UserInformation getUserInfoByEmail(String email) {
		if (!Common.isEmpty(email)) {
			User us = this.userDao.findByEmail(email);
			if (us != null) {
				return this.userInfoDao.getUserInfo(us.getId());
			}
		}
		return null;
	}

	// 小弟的测试用户名是否重名
	@Override
	public boolean Check(String name) {
		return this.getUserDao().Check(name);
	}

	@Override
	public String getUserNameByEmail(String email) {
		if (!Common.isEmpty(email)) {
			User ua = this.userDao.findByEmail(email);
			if (ua != null) {
				return ua.getNickName();
			}
		}
		return null;
	}

	@Override
	public void updateUserInfo(UserInformation userInfo, User user) {
		userInfoDao.update(userInfo);
		userDao.update(user);
	}

	@Override
	public User getUserByEmail(String email) {
		return this.userDao.findByEmail(email);
	}

}
